<?php
namespace app\common\controller;

use think\Controller;
use think\Request;
use classes\Auth;
use think\Session;
use app\common\model\SystemConfig as SystemConfigModel;

use EasyWeChat\Foundation\Application;

/**
 * 微信公用基础控制器
 * Class WeChatBase
 * @package app\common\controller
 */
class WeChatBase extends Controller
{
	protected $app   = null;
	protected $SystemConfigModel;

	protected function _initialize()
	{
		parent::_initialize();

		$this->SystemConfigModel       = new SystemConfigModel();

		$apiInfo = $this->SystemConfigModel->where('name','wechat_api_parameter')->find();
        $weChatApiInfo = json_decode($apiInfo['value'],true);

        $payInfo = $this->SystemConfigModel->where('name','wechat_pay_parameter')->find();
        $weChatPayInfo = json_decode($payInfo['value'],true);

		$options = [
			// api
			'debug'   => true,
			'app_id'  => $weChatApiInfo['wechat_appid'],
			'secret'  => $weChatApiInfo['wechat_appsecret'],
			'token'   => $weChatApiInfo['wechat_token'],
			'aes_key' => $weChatApiInfo['wechat_encodingaeskey'], //  可选
			'log'     => [
				'level' => 'debug',
				'file'  => '/data/www/sfadmin2/easywechat.log', //  绝对路径！！！！
				],
			// payment
		    'payment' => [
		        'merchant_id'        => $weChatPayInfo['wechat_mch_id'],
		        'key'                => $weChatPayInfo['wechat_partnerkey'],
		        'cert_path'          => 'path/to/your/cert.pem', // XXX: 绝对路径！！！！
		        'key_path'           => 'path/to/your/key',      // XXX: 绝对路径！！！！
		        'notify_url'         => '默认的订单回调地址',       // 你也可以在下单时单独设置来想覆盖它
		        // 'device_info'     => '013467007045764',
		        // 'sub_app_id'      => '',
		        // 'sub_merchant_id' => '',
		        // ...
			];

		$this->app   = new Application($options);
	}


}